package com.sch.wanandroid.ui.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sch.wanandroid.network.NetWorkManager
import kotlinx.coroutines.launch

/**
 * Created by Sch.
 * Date: 2020/12/14
 * description:
 */
class LoginViewModel : ViewModel() {
    val userName = MutableLiveData<String>()
    val passWord = MutableLiveData<String>()

    private val _loginSuccess = MutableLiveData<Boolean>()
    val loginSuccessLiveData: LiveData<Boolean> = _loginSuccess

    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> = _errorMsg
    fun doLogin() {
        viewModelScope.launch {
            try {
                val result = NetWorkManager.apiService.doLogin(userName.value!!, passWord.value!!)
                if (result.errorCode != 0) {
                    _errorMsg.value = result.errorMsg
                } else {
                    _loginSuccess.value = true
                }
            } catch (e: Exception) {
                _errorMsg.value = e.toString()
            }
        }
    }
}